package common.util.media;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.imgscalr.Scalr;

import common.Constant;
import common.util.SystemProperties;

import domain.Resolution;

public class ImageUtil {
	
	static public void drawImage (File imageFile, Resolution resol, String storeURl) throws IOException{
		BufferedImage originalImage = ImageIO.read(imageFile);
		
		BufferedImage newImage =Scalr.resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_WIDTH,
				resol.getWeight(),resol.getHeight(), Scalr.OP_ANTIALIAS);
		
		
		
		File newFile = new File(storeURl + imageFile.getName());
		
		ImageIO.write(newImage, getExtension(imageFile), newFile);
	}
	
	static public void drawImage (InputStream inputStream, Resolution resol, String storeURl, String name, String extension) throws IOException{
		BufferedImage originalImage = ImageIO.read(inputStream);
		
		BufferedImage newImage =Scalr.resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_WIDTH,
				resol.getWeight(),resol.getHeight(), Scalr.OP_ANTIALIAS);
		
		
		
		File newFile = new File(storeURl + name + "." + extension);
		
		ImageIO.write(newImage, extension, newFile);
	}
	
	public static void main(String[] args) {
		File orgFile = new File("C:/Users/hung/Favorites/Desktop/a.jpg");
		createThumbnailImage(orgFile);
		
	}
	static public void saveOriginalFile (InputStream inputStream, String name, String originalFilename) throws IOException{
		BufferedImage originalImage = ImageIO.read(inputStream);
		String extension = getExtension(originalFilename);
		
		StringBuffer url = new StringBuffer();
		url.append(SystemProperties.getProperty(Constant.PROPERTIES_NAME.DIRECTORY.IMAGE_PRE))
			.append(SystemProperties.getProperty(Constant.PROPERTIES_NAME.DIRECTORY.SUB_URL.IMAGE.ORIGINAL));
		
		File newFile = new File(url + name + "." + extension);
		
		ImageIO.write(originalImage, extension, newFile);
	}
	
	public static void createThumbnailImage(File imageFile){
		Resolution resol = new Resolution();
		resol.setHeight(100);
		resol.setWeight(100);
		
		StringBuffer url = new StringBuffer();
		url.append(SystemProperties.getProperty(Constant.PROPERTIES_NAME.DIRECTORY.IMAGE_PRE))
			.append(SystemProperties.getProperty(Constant.PROPERTIES_NAME.DIRECTORY.SUB_URL.IMAGE.THUMB));
		
		try {
			drawImage(imageFile,resol,url.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void createThumbnailImage(InputStream inputStream,String name){
		String extension = SystemProperties.getProperty(Constant.PROPERTIES_NAME.FILE_EXTENTION.THUMBNAIL);
		Resolution resol = new Resolution();
		resol.setHeight(100);
		resol.setWeight(100);
		
		StringBuffer url = new StringBuffer();
		url.append(SystemProperties.getProperty(Constant.PROPERTIES_NAME.DIRECTORY.IMAGE_PRE))
			.append(SystemProperties.getProperty(Constant.PROPERTIES_NAME.DIRECTORY.SUB_URL.IMAGE.THUMB));
		
		try {
			drawImage(inputStream,resol,url.toString(),name,extension);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String getExtension(File file){
		return getExtension(file.getName());
	}
	
	public static String getExtension(String fileName){
		String extension = "";

		int i = fileName.lastIndexOf('.');
		if (i > 0) {
		    extension = fileName.substring(i+1);
		}
		return extension;
	}
}
